package vg.services.user_interface_manager.additional_swing_components.conditionPanel.components;


import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

import javax.swing.*;

import vg.services.user_interface_manager.additional_swing_components.Bracket;
import vg.services.user_interface_manager.additional_swing_components.ValueComboBox;

public class RealCondition implements ICondition {
    // Main data
    private Set<String> realAttributeSet;

    // Swing data
    private JComboBox attributeNameComboBox;
    private JTextField leftValueTextField;
    private JTextField rightValueTextField;

    private JButton closeButton; // button for closing (deleting) of condition

    private Bracket leftBracketBox;
    private Bracket rightBracketBox;

    private JPanel view;

    public RealCondition(Set<String> realAttributeSet) {
        // initialize main data
        this.realAttributeSet = realAttributeSet;

        // initialize swing components
        view = new JPanel(new GridBagLayout());

        String[] attributeNames = new String[realAttributeSet.size()];
        attributeNames = realAttributeSet.toArray(attributeNames);
        Arrays.sort(attributeNames);
        attributeNameComboBox = new JComboBox(attributeNames);

        leftValueTextField = new JTextField();
        rightValueTextField = new JTextField();

        // initialize close button
        // TODO: need change text to picture
        closeButton = new JButton("Close");

        // initialize brackets
        leftBracketBox = new Bracket(Bracket.DEF_LEFT_BRACKET);
        rightBracketBox = new Bracket(Bracket.DEF_RIGHT_BRACKET);

        // package user interface
        view.add(closeButton, new GridBagConstraints(0,0,  1,1,  0,0,  GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3),  0,0));
        view.add(leftBracketBox.getView(), new GridBagConstraints(0,1,  1,1,  0,0,  GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3),  0,0));

        view.add(leftValueTextField, new GridBagConstraints(1,1,  1,1,  1,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3),  0,0));
        view.add(new JLabel("<="), new GridBagConstraints(2,1,  1,1,  1,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3),  0,0));
        view.add(attributeNameComboBox, new GridBagConstraints(3,1,  1,1,  1,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3),  0,0));
        view.add(new JLabel("<="), new GridBagConstraints(4,1,  1,1,  1,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3),  0,0));
        view.add(rightValueTextField, new GridBagConstraints(5,1,  1,1,  1,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3),  0,0));

        view.add(rightBracketBox.getView(), new GridBagConstraints(6,1,  1,1,  0,0,  GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3),  0,0));
    }

    public String getName() {
        Object name = attributeNameComboBox.getSelectedItem();
        if(name == null)
            return "";
        return name.toString();
    }

    public String getLeftValue() {
        return leftValueTextField.getText();
    }

    public String getRightValue() {
        return rightValueTextField.getText();
    }

    public String getLeftBracket() {
        return leftBracketBox.getText();
    }

    public String getRightBracket() {
        return rightBracketBox.getText();
    }

    @Override
    public JComponent getView() {
        return view;
    }

    @Override
    public void updateUITheme() {
        view.updateUI();
    }

    @Override
    public void addCloseListener(ActionListener l) {
        closeButton.addActionListener(l);
    }

    @Override
    public void removeCloseListener(ActionListener l) {
        closeButton.removeActionListener(l);
    }
}
